package com.easyedu.dean.service;

import com.easyedu.core.dao.StudentDao;
import com.easyedu.core.entity.po.Student;
import com.easyedu.dean.entity.excel.Student2Excel;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author lmr
 **/
@Service
public class StudentService {

     private final StudentDao studentDao;

     public StudentService(StudentDao studentDao) {
          this.studentDao = studentDao;
     }

     @Transactional(rollbackFor = Exception.class)
     public void insertStudentsFromExcel(List<Student2Excel> list) {
          for (Student2Excel s2 : list) {
               Student s = Student.builder().studentNumber(s2.getStudentNumber()).instituteId(s2.getInstituteId())
                       .instituteName(s2.getInstituteName()).departmentId(s2.getDepartmentId())
                       .departmentName(s2.getDepartmentName()).grade(s2.getGrade())
                       .administrationClass(s2.getAdministrationClass()).statue(s2.getStatue()).name(s2.getName())
                       .sex(s2.getSex()).usedName(s2.getUsedName()).nation(s2.getNation()).politics(s2.getPolitics())
                       .joinParty(s2.getJoinParty()).nativePlace(s2.getNativePlace()).bornTime(s2.getBornTime())
                       .idNumber(s2.getIdNumber()).enrollmentDate(s2.getEnrollmentDate()).dormitory(s2.getDormitory())
                       .phone(s2.getPhone()).origin(s2.getOrigin()).type(s2.getType()).parent1(s2.getParent1())
                       .parent1IdType(s2.getParent1IdType()).parent1IdNumber(s2.getParent1IdNumber())
                       .parent1Phone(s2.getParent1Phone()).parent2(s2.getParent2()).parent2IdType(s2.getParent2IdType())
                       .parent2IdNumber(s2.getParent2IdNumber()).parent2Phone(s2.getParent2Phone()).build();
               Integer integer = studentDao.insertStudentFromExcel(s);
               if (integer == 0) {
                    throw new RuntimeException("插入数据失败");
               }
          }
     }
}
